<?php


class session { 
    // session-lifetime 
    var $lifeTime; 
    // mysql-handle 
    var $dbHandle; 
	
    function open($savePath, $sessName) {
    	
		$result=true;
		
		try {
        										
			// get session-lifetime 
			$this->lifeTime = get_cfg_var("session.gc_maxlifetime"); 
			// open database-connection 
			$this->dbHandle = @mysql_connect(SESSION_DB_HOST,
			   								SESSION_DB_USER,
			   								SESSION_DB_PASS); 
			
			if( !$this->dbHandle )
				throw new AppException( DB_NOT_CONNECTION
										.': host/ip:'.SESSION_DB_HOST );
			
														
			$dbSel = mysql_select_db(SESSION_DB_DATABASE,
	       							$this->dbHandle); 
									
									
			if( !$dbSel )						
				throw new AppException( DB_NOT_CONNECTION
										.': host/ip:'.SESSION_DB_HOST );
				
			
			$result=true;
									
			// return success 
	       // if(!$dbHandle || !$dbSel) 
	           // $result	=	false; 
		   // else
		   	// $this->dbHandle = $dbHandle; 

					
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	        //echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		       
		return $result; 
    } 
	
	
	
    function close() {
    	
		$result=false;
		
		try {
        										
			        	
        	if( $this->dbHandle ){        		
				
				$this->gc(ini_get('session.gc_maxlifetime')); 
        		// close database-connection 
        	
        		$result	=	mysql_close($this->dbHandle); 
        	}
				
					
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	        //echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }

 		return $result;
		
        
    } 
	
	
    function read($sessID) {
    	
		$result	=	"";
		
		try {
			
        	
			if( $this->dbHandle ){
				
				 // fetch session-data 
		        $res = mysql_query("SELECT session_data AS d FROM ".SESSION_DB_TABLE_SESSION." 
		                            WHERE session_id = '$sessID' 
		                            AND session_expires > ".time(),$this->dbHandle); 
		        // return data or an empty string at failure 
		        if($row = mysql_fetch_assoc($res)) 
		            $result	= $row['d']; 
				
			}else
				throw new AppException( DB_NOT_CONNECTION
										.': host/ip:'.SESSION_DB_HOST );

						
	    }
	    catch (AppException $e) {
	    	//throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
			 
       
        return $result;
    } 
	
	
    function write($sessID,$sessData) {
    	
		$result=false;
		
		try {
        										
			 // new session-expire-time 
	        $newExp = time() + $this->lifeTime; 
			
			if( $this->dbHandle ){
		        // is a session with this id in the database? 
		        $res = @mysql_query("SELECT * FROM ".SESSION_DB_TABLE_SESSION."  
		                            WHERE session_id = '$sessID'",$this->dbHandle); 
		        // if yes, 
		        if(@mysql_num_rows($res)) { 
		            // ...update session-data 
		            mysql_query("UPDATE ".SESSION_DB_TABLE_SESSION."  
		                         SET session_expires = '$newExp', 
		                         session_data = '$sessData' 
		                         WHERE session_id = '$sessID'",$this->dbHandle); 
		            // if something happened, return true 
		            if(mysql_affected_rows($this->dbHandle)) 
		                $result = true; 
		        } 
		        // if no session-data was found, 
		        else { 
		            // create a new row 
		            @mysql_query("INSERT INTO ".SESSION_DB_TABLE_SESSION."  ( 
		                         session_id, 
		                         session_expires, 
		                         session_data) 
		                         VALUES( 
		                         '$sessID', 
		                         '$newExp', 
		                         '$sessData')",$this->dbHandle); 
		            // if row was created, return true 
		            if(@mysql_affected_rows($this->dbHandle)) 
		                $result = true; 
		        } 
				
				
			}else
				throw new AppException( DB_NOT_CONNECTION
										.': host/ip:'.SESSION_DB_HOST );	
							
	    }
	    catch (AppException $e) {
	    	//throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		
		 
       
        // an unknown error occured 
        return $result; 
    } 


    function destroy($sessID) {
    	
		$result=false;
		
		try {
	
			if( $this->dbHandle ){
				// delete session-data 
		        mysql_query("DELETE FROM "
		        			.SESSION_DB_TABLE_SESSION
		        			."  WHERE session_id = '$sessID'",$this->dbHandle); 
		        // if session was deleted, return true, 
		        if(mysql_affected_rows($this->dbHandle)) 
		            $result	=	true; 
		        // ...else return false 	
		        
			}else
				throw new AppException( DB_NOT_CONNECTION
										.': host/ip:'.SESSION_DB_HOST );
										
	        
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		        
        return $result; 
    } 
	
	
    function gc($sessMaxLifeTime) {
    	
		$result=false;
		
		try {
        	
			if( $this->dbHandle ){
													
				// delete old sessions 
	        	mysql_query("DELETE FROM ".SESSION_DB_TABLE_SESSION."  WHERE session_expires < ".time(),$this->dbHandle); 
	        	// return affected rows 
	        	$result	=	mysql_affected_rows($this->dbHandle); 	
				
			}else
				throw new AppException( DB_NOT_CONNECTION
										.': host/ip:'.SESSION_DB_HOST );
										
					
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	        //echo "#1Caught TestException ('{$e->getMessage()}')\n{$e}\n";
	    }
	    catch (Exception $e) {
	        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
	    }
		
		 
        return $result;
    } 
} 

?>